package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.base.DateHandler;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.common.office.EchartsHandler;
import com.theme.passport.dao.theme.TradeCollectDao;
import com.theme.passport.entity.constant.TradeCollectTypeEnum;
import com.theme.passport.entity.theme.TradeCollectEntity;
import com.theme.passport.service.theme.TradeCollectService;
import com.theme.passport.service.theme.UserPayOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tradeCollectService")
public class TradeCollectServiceImpl extends ServiceImpl<TradeCollectDao, TradeCollectEntity> implements TradeCollectService {

    @Autowired
    private UserPayOrderService userPayOrderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String type = (String) params.get("type");
        IPage<TradeCollectEntity> page = this.page(
                new Query<TradeCollectEntity>().getPage(params),
                new QueryWrapper<TradeCollectEntity>()
                        .eq(StringUtils.isNotBlank(type), "type", type)
                        .orderByDesc("created")
        );

        return new PageUtils(page);

    }

    @Override
    public void tradeCollect() {
        String formatDate = "yyyy-MM-dd";
        SimpleDateFormat formatter = new SimpleDateFormat(formatDate);
        //1、统计当天的所有交易金额 sql
        // 获取开始时间和结束时间
        String currentDate = formatter.format(new Date());
        Date startDate = null;
        try {
            startDate = DateHandler.otherDate(currentDate, formatDate, -1);
            String startTime = formatter.format(startDate) + " 00:00:00";
            String endTime = formatter.format(startDate) + " 23:59:00";
            Map<String, Object> params = new HashMap<>();
            params.put("startTime", startTime);
            params.put("endTime", endTime);

            Long  totalPrice = userPayOrderService.tradeCollectPrice(params);

            //2、入库到新表 入库
            TradeCollectEntity tradeCollectEntity = new TradeCollectEntity();
            tradeCollectEntity.setType(TradeCollectTypeEnum.ALL.getValue());
            tradeCollectEntity.setMoney(totalPrice);
            tradeCollectEntity.setCreated(startDate);

            this.save(tradeCollectEntity);

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Map<String, Object> queryTradeCollectEcharts(Byte dateType) {
        List<Map<String, Object>> userRegisterLineChart = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String, Object> params = new HashMap<>();
            Date startTime = EchartsHandler.getStartTimeByDateType(dateType);
            Date endTime = EchartsHandler.getEndTimeByDateType(dateType);
            params.put("startTime", formatter.format(startTime));
            params.put("endTime", formatter.format(endTime));
            if (dateType == 1) {
                //1、按当日查询-每个小时段
                userRegisterLineChart = baseMapper.queryUserLineChartByHours(params);
            } else if (dateType == 2) {
                //2、按天查询-近七天
                userRegisterLineChart = baseMapper.queryUserLineChartByDays(params);
            } else if (dateType == 3) {
                //3、按天查询-近30天
                userRegisterLineChart = baseMapper.queryUserLineChartByMonths(params);
            } else if (dateType == 4) {
                //4、按天查询-当月每一天
                userRegisterLineChart = baseMapper.queryUserLineChartByDays(params);
            } else if (dateType == 5) {
                //5、按月查询-当年每个月
                userRegisterLineChart = baseMapper.queryUserLineChartByDays(params);
            }
            return EchartsHandler.completionDate(userRegisterLineChart, Integer.valueOf(dateType));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}